﻿using Corina.DataContext.Master;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Services;

namespace Corina.Webservice.Cydee
{
    /// <summary>
    /// Summary description for Uploader
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Uploader : WebserviceBase
    {
        [WebMethod]
        public ResultInfo UploadGBFile(string account, string password, string documentName, string guobiaoName, string code, string description, string contentString, bool deleteExist)
        {
            string folder = WebConfigurationManager.AppSettings["directory_upload"] + "Guobiao\\";
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            try
            {
                byte[] bytes = Convert.FromBase64String(contentString);
                string filename = folder + documentName;
                if (File.Exists(filename) && deleteExist)
                {
                    File.Delete(filename);
                }
                File.WriteAllBytes(filename, bytes);
                NationalStandardInfo entity = new NationalStandardInfo() { Code = code, Description = description, DocumentName = documentName, Name = guobiaoName };
                return GetInstance<NationalStandardMaster>().Insert(account, password, entity);
            }
            catch (Exception ex)
            {
                return new ResultInfo(ResultValue.Failed) { Description = ex.Message };
            }
        }

        [WebMethod]
        public ResultInfo AppendToFile(string filename, string folder, bool isfirst, bool replace, string base64String)
        {
            ResultInfo result = new ResultInfo();
            string _folder = WebConfigurationManager.AppSettings["directory_upload"] + folder + "\\";
            try
            {
                if (!Directory.Exists(_folder))
                {
                    Directory.CreateDirectory(_folder);
                }

                string fullname = _folder + filename;

                if (isfirst && replace)
                {
                    if (File.Exists(fullname))
                    {
                        File.Delete(fullname);
                    }
                }

                byte[] bytes = Convert.FromBase64String(base64String);

                using (var stream = new FileStream(fullname, FileMode.Append))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
                result.Result = ResultValue.Succeed;
            }
            catch (Exception ex)
            {
                result.Result = ResultValue.Failed;
                result.Description = ex.Message;
            }
            return result;
        }
    }
}
